package com.ycy.socket;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author PanBingYu
 * @description
 * @date 2022-07-13 19:08
 */

@Component
@Slf4j
public class HomePageSocketIo extends BaseSocketIoServer {

  public static final String EVENT_ALARM_OCCUR = "ALARM_OCCUR";

  public static final String DEFAULT_CORP_CODE = "CORP_CODE";


  @Override
  public String getCustomClientKey(SocketIOClient client) {
    return client.getHandshakeData().getSingleUrlParam("uuid");
  }

  @Override
  public void addEventListener(SocketIOServer socketIOServer) {
    socketIOServer.addEventListener(EVENT_ALARM_OCCUR, Object.class, (client, data, ackSender) -> {
      log.info("{}", data);
    });
  }

  /**
   * 现在是非saas的。
   * 但是代码留有了可saas的口子，所以所有连接先都使用同一个CORP_CODE
   */
  @Override
  public ConnectListener getConnectListener() {
    return client -> {
      String key = this.getCustomClientKey(client);

      String token = client.getHandshakeData().getSingleUrlParam(TOKEN);

      // 保存进client的临时存储里，方便后续调用
      saveCorpCode(client, DEFAULT_CORP_CODE);

//            if (StringUtils.isBlank(key)) {
//                return;
//            }
      getClientCache().putCache(key, DEFAULT_CORP_CODE, client);
    };
  }


}
